package com.xxx.mall.api.admin;

import com.xxx.mall.dto.AttrDTO;
import com.xxx.mall.dto.PageQuery;
import com.xxx.mall.response.R;
import com.xxx.mall.service.AttrService;
import com.xxx.mall.vo.AttrVO;
import com.xxx.mall.vo.PageVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 邢晨旭
 * {@code @date} 2023/11/23
 */
@RestController
@RequestMapping("/admin/attr")
public class AttrApi {

    @Resource
    AttrService attrService;

    @GetMapping("/page")
    public R<PageVO<AttrVO>> page(@Valid PageQuery pageQuery, AttrDTO attrDTO) {
        return R.ok(attrService.pageList(pageQuery, attrDTO));
    }

    @GetMapping("/base_list")
    public R<List<AttrVO>> list(@RequestParam Long categoryId) {
        return R.ok(attrService.listByCategoryId(categoryId,1));
    }

    @GetMapping("/sale_list")
    public R<List<AttrVO>> saleList(@RequestParam Long categoryId) {
        return R.ok(attrService.listByCategoryId(categoryId,0));
    }

    @PostMapping
    public R<Void> save(@RequestBody @Valid AttrDTO attrDTO) {
        attrService.saveAttr(attrDTO);
        return R.ok();
    }

    @PutMapping
    public R<Void> update(@RequestBody @Valid AttrDTO attrDTO) {
        attrService.updateAttr(attrDTO);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R<AttrVO> getById(@PathVariable Long id) {
        return R.ok(attrService.getAttrById(id));
    }

    @DeleteMapping("/{id}")
    public R<Void> del(@PathVariable Long id) {
        attrService.delAttrById(id);
        return R.ok();
    }

}
